<template>
  <div class="app-container">
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="listQuery.Filter" clearable size="small" placeholder="搜索..." style="width: 200px;"
        class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search"
        @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-edit"
        @click="handleUpdate()">修改</el-button>
      <el-button slot="reference" class="filter-item" type="danger" icon="el-icon-delete" size="mini"
        @click="handleDelete()">删除</el-button>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="false" @close="cancel()" :title="formTitle">

      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
          <el-col :span="12">
            <el-form-item label="仓库编号" prop="warehouseNo">
              <el-input v-model="form.warehouseNo" placeholder="可自动生成" clearable :style="{ width: '100%' }"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门" prop="departmentId">
              <el-select v-model="form.departmentId" placeholder="请选择部门" clearable :style="{ width: '100%' }">
                <el-option v-for="(item, index) in DepartmentIdOptions" :key="index" :label="item.label"
                  :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="warehouseName">
              <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" clearable
                :style="{ width: '100%' }"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库类型" prop="warehouseType">
              <el-select v-model="form.warehouseType" placeholder="请选择仓库类型" clearable :style="{ width: '100%' }">
                <el-option v-for="(item, index) in WarehouseTypeOptions" :key="index" :label="item.label"
                  :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="作用" prop="purpose">
              <el-input v-model="form.purpose" placeholder="请输入作用" clearable :style="{ width: '100%' }"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租赁时间" prop="leaseTime">
              <el-date-picker type="datetime" v-model="form.leaseTime" format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss" :style="{ width: '100%' }" placeholder="请选择时间选择"
                clearable></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="面积" prop="area">
              <el-input-number v-model="form.area"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" clearable :style="{ width: '100%' }"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入联系人" clearable :style="{ width: '100%' }"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" clearable :style="{ width: '100%' }"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button size="small" type="text" @click="cancel">取消</el-button>
        <el-button size="small" v-loading="formLoading" type="primary" @click="save">确认</el-button>
      </div>
    </el-dialog>
    <el-table ref="multipleTable" v-loading="listLoading" :data="list" size="small" style="width: 90%;"
      @sort-change="sortChange" @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="44px"></el-table-column>
      <el-table-column label="仓库编号" prop="warehouseNo" align="center" />
      <el-table-column label="部门" prop="departmentId" align="center">
        <template v-slot="scope">
          <span>{{ getDepartmentName(scope.row.departmentId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库名称" prop="warehouseName" align="center" />
      <el-table-column label="仓库类型" prop="warehouseType" align="center">
        <template v-slot="scope">
          <span>{{ getWarehouseTypeName(scope.row.warehouseType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作用" prop="purpose" align="center" />
      <el-table-column label="租赁时间" prop="leaseTime" align="center" >
        <template v-slot="scope">
          <span>{{ scope.row.leaseTime ? scope.row.leaseTime.substr(0, 10) : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="面积" prop="area" align="center" />
      <el-table-column label="地址" prop="address" align="center" />
      <el-table-column label="联系人" prop="contact" align="center" />
      <el-table-column label="电话" prop="phone" align="center" />
      <el-table-column label="操作" align="center" width="160px">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)" icon="el-icon-edit" />
          <el-button type="danger" size="mini" @click="handleDelete(row)" icon="el-icon-delete" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="totalCount > 0" :total="totalCount" :page.sync="page" :limit.sync="listQuery.MaxResultCount"
      @pagination="getList" />
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
import { get } from "lodash";
const defaultForm = {
  id: null,
  warehouseNo: null,
  departmentId: null,
  warehouseName: null,
  warehouseType: null,
  purpose: null,
  leaseTime: null,
  area: 0,
  address: null,
  contact: null,
  phone: null,
}
export default {
  name: 'Warehouse',
  components: {
    Pagination
  },
  directives: {
    permission
  },
  props: [],
  data() {
    return {
      rules: {
        warehouseNo: [],
        departmentId: [],
        warehouseName: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
        warehouseType: [],
        purpose: [],
        leaseTime: [],
        area: [],
        address: [],
        contact: [],
        phone: [],
      },
      form: Object.assign({}, defaultForm),
      list: null,
      totalCount: 0,
      listLoading: true,
      formLoading: false,
      listQuery: {
        Filter: '',
        Sorting: '',
        SkipCount: 0,
        MaxResultCount: 10
      },
      page: 1,
      dialogFormVisible: false,
      multipleSelection: [],
      formTitle: '',
      isEdit: false,
      // 部门列表
      DepartmentIdOptions: null,
      WarehouseTypeOptions: null,
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList()
    this.getDepartmentList()
    this.getWarehouseTypeOptions()

  },
  mounted() { },
  methods: {
    // 获取部门名称
    getDepartmentName(id) {
      const department = this.DepartmentIdOptions.find(item => item.value === id);
      return department ? department.label : '';
    },
    // 获取仓库类型名称
    getWarehouseTypeName(id) {
      const warehouseType = this.WarehouseTypeOptions.find(item => item.value === id);
      return warehouseType ? warehouseType.label : '';
    },
    //仓库类型
    getWarehouseTypeOptions() {
      this.$axios.gets('/api/base/dictDetails/all?Pid=7d1509ed-acb0-108c-0220-3a16c143556b').then(response => {
        this.WarehouseTypeOptions = response.items.map(item => {
          return {
            label: item.label,
            value: parseInt(item.value),
            disabled: false
          }
        })
      });
    },
    // 获取部门列表
    getDepartmentList() {
      this.$axios.gets('/api/base/orgs/loadOrgs?id=e01f7a7d-5736-3ebb-c2ae-3a16c13e6beb').then(response => {
        this.DepartmentIdOptions = response.items.map(item => {
          return {
            label: item.name,
            value: item.sort,
            disabled: false
          }
        })
      });
    },
    getList() {
      this.listLoading = true;
      this.listQuery.SkipCount = (this.page - 1) * this.listQuery.MaxResultCount;
      this.$axios.gets('/api/business/Warehouse', this.listQuery).then(response => {
        this.list = response.items;
        this.totalCount = response.totalCount;
        this.listLoading = false;
      });
    },
    fetchData(id) {
      this.$axios.gets('/api/business/Warehouse/' + id).then(response => {
        this.form = response;
      });
    },
    handleFilter() {
      this.page = 1;
      this.getList();
    },
    handleCreate() {
      this.getWarehouseTypeOptions()
      this.getDepartmentList()
      this.formTitle = '新增仓库';
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    handleDelete(row) {
      var params = [];
      let alert = '';
      if (row) {
        params.push(row.id);
        alert = row.name;
      }
      else {
        if (this.multipleSelection.length === 0) {
          this.$message({
            message: '未选择',
            type: 'warning'
          });
          return;
        }
        this.multipleSelection.forEach(element => {
          let id = element.id;
          params.push(id);
        });
        alert = '选中项';
      }
      this.$confirm('是否删除' + alert + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.posts('/api/business/Warehouse/delete', params).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.getList();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleUpdate(row) {
      this.getWarehouseTypeOptions()
      this.getDepartmentList()
      this.formTitle = '修改仓库';
      this.isEdit = true;
      if (row) {
        this.fetchData(row.id);
        this.dialogFormVisible = true;
      }
      else {
        if (this.multipleSelection.length != 1) {
          this.$message({
            message: '编辑必须选择单行',
            type: 'warning'
          });
          return;
        }
        else {
          this.fetchData(this.multipleSelection[0].id);
          this.dialogFormVisible = true;
        }
      }
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.formLoading = true;
          this.form.roleNames = this.checkedRole;
          if (this.isEdit) {
            this.$axios.posts('/api/business/Warehouse/data-post', this.form).then(response => {
              this.formLoading = false;
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.getList();
            }).catch(() => {
              this.formLoading = false;
            });
          }
          else {
            this.$axios.posts('/api/business/Warehouse/data-post', this.form).then(response => {
              this.formLoading = false;
              this.$notify({
                title: '成功',
                message: '新增成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.getList();
            }).catch(() => {
              this.formLoading = false;
            });
          }
        }
      });
    },
    sortChange(data) {
      const {
        prop,
        order
      } = data;
      if (!prop || !order) {
        this.handleFilter();
        return;
      }
      this.listQuery.Sorting = prop + ' ' + order;
      this.handleFilter();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    cancel() {
      this.form = Object.assign({}, defaultForm);
      this.dialogFormVisible = false;
      this.$refs.form.clearValidate();
    },
  }
}

</script>
<style></style>
